import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;

/**
 * GameManager 游戏控制管理类
 *
 * @author 烦得很
 */
public class GameManager {
    private static Rendering gameRender;
    private SnackManager snackManager;
    private int scores = 0; // 分数
    private boolean isGameOver = false;
    private boolean isHaveApple = false;
    private SnackManager.snackMoveDirs nowMoveDir = SnackManager.getSnackNowMoveDir(); // 初始化蛇的移动方向

    /**
     * 获取Rendering的实例
     *
     * @return Rendering的实例
     */
    public static Rendering getGameRender() {
        return gameRender;
    }

    /**
     * 初始化
     */
    public GameManager() {
        snackManager = new SnackManager();
        inputMapSizeDialog();
        //初始化按键监听
        updateMoveDir();
    }

    /**
     * 获取现在蛇的移动方向
     *
     * @return 蛇现在的移动方向
     */
    public SnackManager.snackMoveDirs getSnackNowMoveDir() {
        return nowMoveDir;
    }

    /**
     * 前言和自定义设置地图大小
     */
    private void inputMapSizeDialog() {
        JOptionPane.showMessageDialog(null, "本项目为学习类型项目", "前言", JOptionPane.INFORMATION_MESSAGE);
        JOptionPane.showMessageDialog(null,"建议方向键移动，wasd也可但不推荐","操作控制",JOptionPane.INFORMATION_MESSAGE);
        int choice = JOptionPane.showConfirmDialog(null, "是否自定义地图大小\n默认大小为20 * 20", "游戏基本设置",
                JOptionPane.YES_NO_OPTION);
        if (choice == 1) {
            gameRender = new Rendering(); // Rendering实例化
        } else {
            while (true) {
                String input = JOptionPane.showInputDialog(null, "请输入地图大小\n(输入一个大于等于10的数值,生成正方形地图):", "自定义地图",
                        JOptionPane.PLAIN_MESSAGE);
                input = input.trim();
                try {
                    int mapSize = Integer.parseInt(input);
                    if (mapSize < 10) {
                        throw new NumberFormatException();
                    }
                    gameRender = new Rendering(mapSize); // Rendering实例化
                    break;
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "输入不规范", "警告", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

    /**
     * 全局按键监听
     */
    public void updateMoveDir() {
        nowMoveDir = SnackManager.getSnackNowMoveDir();
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(e -> {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_W:
                    case KeyEvent.VK_UP:
                        if (nowMoveDir != SnackManager.snackMoveDirs.down) {
                            nowMoveDir = SnackManager.snackMoveDirs.up;
                        }
                        break;
                    case KeyEvent.VK_S:
                    case KeyEvent.VK_DOWN:
                        if (nowMoveDir != SnackManager.snackMoveDirs.up) {
                            nowMoveDir = SnackManager.snackMoveDirs.down;
                        }
                        break;
                    case KeyEvent.VK_A:
                    case KeyEvent.VK_LEFT:
                        if (nowMoveDir != SnackManager.snackMoveDirs.right) {
                            nowMoveDir = SnackManager.snackMoveDirs.left;
                        }
                        break;
                    case KeyEvent.VK_D:
                    case KeyEvent.VK_RIGHT:
                        if (nowMoveDir != SnackManager.snackMoveDirs.left) {
                            nowMoveDir = SnackManager.snackMoveDirs.right;
                        }
                        break;
                    default:
                        break;
                }
            }
            return false;
        });
    }

    // private boolean isAppleEat = false;

    // private static int scores = 0;

    // public static int GetScores() {
    // return scores;
    // }

    // public static void SetScores(int score) {
    // scores = score;
    // }

    /**
     * 刷新游戏
     */
    public void update() {
        if (!isGameOver) {
            gameRender.requestForce();
            // 判断是否需要放置苹果
            if (!isHaveApple) {
                gameRender.putApple();
                isHaveApple = true;
            }
            // 获取蛇的移动方向
            SnackManager.snackMoveDirs moveDir = nowMoveDir;
            snackManager.setSnackNowMoveDir(moveDir);
            snackManager.snackMove(moveDir);
            // snackHead = snackManager.getSnackHead();
            int[] snackHead = snackManager.getSnackHead();
            Color targetColor = gameRender.drawSnackHead(snackHead[0], snackHead[1]);
            gameRender.drawSnackAllBody(snackManager.getSnackAllNodes(), snackManager.getTop());
            // 判断是否死亡
            if (isDied(targetColor)) {
                isGameOver = true;
                gameRender.gameOverUIChange();
                return;
            }
            // 判断是否吃到苹果
            if (targetColor.equals(gameRender.getAppleColor())) {
                gameRender.scoreUIChange(++scores);
                snackManager.addSnackBody(); // 增加蛇身
                isHaveApple = false;
            }
        }
    }

    /**
     * 返回分数
     *
     * @return 分数
     */
    public int getScores() {
        return scores;
    }

    /**
     * 判断是否死亡
     *
     * @param targetColor 与蛇头接触的色块
     * @return 是否死亡
     */
    public boolean isDied(Color targetColor) {
        return targetColor.equals(gameRender.getSnackBodyColor()) || targetColor.equals(gameRender.getWallColor());
    }


}